﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;
using System.Data;
public partial class Administrator_ProductSlideEdit : System.Web.UI.Page
{


    ProductSlide objSlide = new ProductSlide();
    ConnectSQL cnts = new ConnectSQL();
    BindToControls objBind = new BindToControls();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadProduct();
        }
    }
    public void LoadProduct()
    {
        DataTable db = cnts.GetTableWithCommandText("select * from tblProducts");
        objBind.BinDDL(ddlProduct, db, "PRODUCT_NAME", "PRODUCT_ID");
        DataTable db2 = cnts.GetTableWithCommandText("select * from tblSlideProduct where SLIDE_ID=" + SLIDE_ID);
        imgDescription.ImageUrl = "~/ProductSlide/" + db2.Rows[0]["IMAGE"].ToString();
        ddlProduct.SelectedValue = db2.Rows[0]["PRODUCT_ID"].ToString();
        txtTitle.Text = db2.Rows[0]["DESCRIPTION"].ToString();

    }
    string SLIDE_ID
    {
        get
        {
            return Request.QueryString["SLIDE_ID"].ToString();
        }
    }

    public int GetRandom()
    {
        Random rd = new Random();
        int a = rd.Next(1, 100);
        return a;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable db2 = cnts.GetTableWithCommandText("select * from tblSlideProduct where SLIDE_ID=" + SLIDE_ID);
            string filname = db2.Rows[0]["IMAGE"].ToString();
            if (fuImage.HasFile)
            {
                filname = GetRandom() + "" + fuImage.FileName;
                fuImage.SaveAs(Server.MapPath("~/ProductSlide/") + filname);
            }

            int product_id = int.Parse(ddlProduct.SelectedValue);

            objSlide.tblSlideProduct_Update(SLIDE_ID,product_id, filname, txtTitle.Text);
            divMessage.InnerHtml = O2S_Message.Success("Hệ thống", "Thực hiện thành công");


        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ thống", "Phát hiện lỗi" + ex.Message);
        }

    }
}